<?php

namespace falcon5\inc\Infrastructure\Services\Falcon5;

use falcon5\inc\Exceptions\RemoteServiceException;
use lucatume\DI52\ContainerException;

trait CacheTrait
{
    /**
     * @param int $pageId
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function lockPage(int $pageId)
    {
        $body = [
            'page_id' => $pageId,
        ];
        $response = $this->getClient()->lockPage($body);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }

    /**
     * @param int $pageId
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function unlockPage(int $pageId)
    {
        $body = [
            'page_id' => $pageId,
        ];
        $response = $this->getEngineDefaultClient()->unlockPage($body);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }
        
        return json_decode($content, true);
    }
}